package com.jiyinit.web.core.mobile.controller;

import com.alibaba.fastjson.JSON;
import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.web.core.business.dto.MChatRequest;
import com.jiyinit.web.core.business.entity.TabCoreGroupEntity;
import com.jiyinit.web.core.business.entity.TabUserGroupEntity;
import com.jiyinit.web.core.business.service.TaskMessageServiceImpl;
import com.jiyinit.web.core.business.service.UserGroupServiceImpl;
import com.jiyinit.web.core.common.config.PropertyPlaceholderConfigurer;
import com.jiyinit.web.core.common.logic.ParamCheckLogic;
import com.jiyinit.web.core.common.util.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
  * 聊天相关接口，获取聊天记录
  * @author liuzongguang
  * @create 2018/7/4 9:03
  */

@Controller
@RequestMapping("/mobile/chat")
public class MChatController {

    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MChatController.class);

    @Resource(name = "core.paramCheckLogic")
    private ParamCheckLogic paramCheckLogic;

    @Resource(name = "core.taskMessageServiceImpl")
    private TaskMessageServiceImpl taskMessageService;

    @Resource(name = "core.userGroupServiceImpl")
    private UserGroupServiceImpl userGroupService;

      /**
       * 
       * @author liuzongguang
       * @create 2018/7/4 9:10
       * @param
       * @return
       */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "getChatHistoryMsg")
    public JSON getChatHistoryMsg(HttpServletRequest request){
        String path = (String) PropertyPlaceholderConfigurer.getContextProperty("IM_URL");
        MChatRequest messageRequest = this.getJsonToRequest(request);
        Map<String, String> headers=new HashMap<String, String>();
        headers.put("token",messageRequest.getToken());
        headers.put("sign",messageRequest.getSign());
        List<TabUserGroupEntity> group = userGroupService.findBySelective(messageRequest);//获取进群时间 群组id
        StringBuffer sbf = new StringBuffer();
        int size = group.size();
        for(int i = 0;i < size;i++){
            TabUserGroupEntity entity = group.get(i);
            if(i == (size - 1 )){
                sbf.append(entity.getGroupId());
            }else{
                sbf.append(entity.getGroupId()+",");
            }
        }
        messageRequest.setGroupIdstr(sbf.toString());
        String paramsStr = JsonEntityTransform.object2Json(messageRequest);
        String result =  HttpClientUtil.post(path + "/msg/getChatHistoryMsg", paramsStr, headers);
        return JSON.parseObject(result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "getSingleChatHistoryMsg")
    public JSON getSingleChatHistoryMsg(HttpServletRequest request){
        String path = (String) PropertyPlaceholderConfigurer.getContextProperty("IM_URL");
        MChatRequest messageRequest = this.getJsonToRequest(request);
        String paramsStr = JsonEntityTransform.object2Json(messageRequest);
        Map<String, String> headers=new HashMap<String, String>();
        headers.put("token",messageRequest.getToken());
        headers.put("sign",messageRequest.getSign());
        String result =  HttpClientUtil.post(path + "/msg/getSingleChatHistoryMsg", paramsStr, headers);
        return JSON.parseObject(result);
    }

    private MChatRequest getJsonToRequest(HttpServletRequest request){
        String logJson = request.getParameter("request_json");
        MChatRequest taskRequest=JsonEntityTransform.json2Object(logJson, MChatRequest.class);
        return taskRequest;
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "getSystemTime")
    public ResponseEntity<ResponseDto> getSystemTime(HttpServletRequest request){
        String path = (String) PropertyPlaceholderConfigurer.getContextProperty("IM_URL");
        String logJson = request.getParameter("request_json");
        MChatRequest requst=JsonEntityTransform.json2Object(logJson, MChatRequest.class);
        Map<String, String> headers=new HashMap<String, String>();
        headers.put("token",requst.getToken());
        headers.put("sign",requst.getSign());
        String result =  HttpClientUtil.post(path + "/msg/getSystemTime", null, headers);
        return new ResponseEntity<>(ResponseDto.responseOK(result), HttpStatus.OK);
    }

    /**
     * 查询群组消息详情
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "groupMsgList")
    public JSON groupMsgList(HttpServletRequest request){
        String path = (String) PropertyPlaceholderConfigurer.getContextProperty("IM_URL");
        MChatRequest messageRequest = this.getJsonToRequest(request);
        String paramsStr = JsonEntityTransform.object2Json(messageRequest);
        Map<String, String> headers=new HashMap<String, String>();
        headers.put("token",messageRequest.getToken());
        headers.put("sign",messageRequest.getSign());
        String result =  HttpClientUtil.post(path + "/msg/groupMsgList", paramsStr, headers);
        return JSON.parseObject(result);
    }

    /**
     * 群聊当前页面收到消息更新时间
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "groupMsgTime")
    public JSON groupMsgTime(HttpServletRequest request){
        String path = (String) PropertyPlaceholderConfigurer.getContextProperty("IM_URL");
        MChatRequest messageRequest = this.getJsonToRequest(request);
        String paramsStr = JsonEntityTransform.object2Json(messageRequest);
        Map<String, String> headers=new HashMap<String, String>();
        headers.put("token",messageRequest.getToken());
        headers.put("sign",messageRequest.getSign());
        String result =  HttpClientUtil.post(path + "/msg/groupMsgTime", paramsStr, headers);
        return JSON.parseObject(result);
    }
}
